<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Service;

use App\Dao\MaterialDao;

class MaterialService extends AbstractService
{
    /**
     * @var MaterialDao
     */
    public $dao;

    public function __construct(MaterialDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 通过classId获取素材列表.
     */
    public function getListByClass(int $class_id, ?array $params = null): array
    {
        $query = $this->dao->model::query();

        $paginate = $query->where('class_id', $class_id)->paginate(
            intval($params['pageSize'] ?? 10),
            ['*'],
            'page',
            intval($params['page'] ?? 1)
        );

        return $this->dao->setPaginate($paginate);
    }

    /**
     * @return mixed
     */
    public function getById(int $id)
    {
        return $this->dao->model::findOrFail($id);
    }
}
